<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017 https://git.oschina.net/honoryao All rights reserved.
// +----------------------------------------------------------------------
// | Author: honoryao <2463523099@qq.com>-2017/8/17
// +----------------------------------------------------------------------
// | app\home\controller-Section
// +----------------------------------------------------------------------

/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/8/17
 * Time: 15:00
 */

namespace app\home\controller;


use app\common\controller\Base;

class Section extends Base
{
    protected $section;
    protected $staff;
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->section=model('Section');
        $this->staff=model('User');
    }

    public function index()
    {
        $this->veryauth();//验证权限
        return $this->fetch();
    }

    public function sectionlist()
    {
        $this->veryauth(1);//验证权限
        $pagenum = intval(input('get.pageNumber'));
        $pageSize = intval(input('get.pageSize'));
        $sortName = input('get.sortName');
        $sortOrder = input('get.sortOrder');
        if ($pagenum == '' || $pagenum <= 0) {
            $pagenum = 1;
        }
        $sortwhere = $sortName . ' ' . $sortOrder;
        $sortwhere = $sortName == '' ? ' id asc ' : $sortwhere;
        $retArray['total']= $this->section->count();
        $retArray['rows'] = $this->section->order($sortwhere)->page($pagenum, $pageSize)->select();

        return json($retArray);
    }

    public function addsection()
    {
        $this->veryauth(1);//验证权限
        $post=input('post.');
        $ret=$this->section->addSection($post);
        return json($ret);

    }

    public function delsection()
    {
        $this->veryauth(1);//验证权限
        $id=input('id');
        $answer=$this->staff->where(['section'=>['in',$id]])->find();
        if ($answer){
            $retArray = ['rel'=>0,'msg'=>'部门下面有员工~'];
        }else{
            $del=$this->section->where(['id'=>['in',$id]])->delete();
            if ($del){
                $retArray = ['rel'=>1,'msg'=>'删除成功'];
            }else{
                $retArray = ['rel'=>0,'msg'=>'删除失败'];
            }
        }
        return json($retArray);
    }

    public function staff()
    {
        $this->veryauth();//验证权限
        return $this->fetch();
    }


    public function stafflist()
    {
        $this->veryauth(1);//验证权限
        $pagenum = intval(input('get.pageNumber'));
        $pageSize = intval(input('get.pageSize'));
        $sortName = input('get.sortName');
        $sortOrder = input('get.sortOrder');

        $myname = trim(input('get.myname'));
        $mail = trim(input('get.mail'));
        $secid=intval(input('secid'));

        if ($pagenum == '' || $pagenum <= 0) {
            $pagenum = 1;
        }
        $sortwhere = $sortName . ' ' . $sortOrder;
        $sortwhere = $sortName == '' ? '  id asc  ' : $sortwhere;

        $where=[];
        if (!empty($secid)){
            $where['a.section']=$secid;
        }
        if (!empty($myname)){
            $where['a.name']=['like','%'.$myname.'%'];
        }
        if (!empty($mail)){
            $where['a.email']=$mail;
        }
        $retArray['total'] = $this->staff->alias('a')->join('_Section_ s','a.section = s.id')->where($where)->count();
        $retArray['rows'] = $this->staff->alias('a')->where($where)->join('_Section_ s','a.section = s.id')->field('a.id,a.name,a.email,s.name as section_name,a.section,a.work_date,a.update_time')->order($sortwhere)->page($pagenum, $pageSize)->select();

        return json($retArray);
    }

    public function addstaff()
    {
        $this->veryauth(1);//验证权限
        $post=input('post.');
        $ret=$this->staff->addUser($post);
        return json($ret);
    }

    public function delstaff()
    {
        $this->veryauth(1);//验证权限
        $id=input('id');
        $del=$this->staff->where(['id'=>['in',$id]])->delete();
        if ($del){
            $retArray = ['rel'=>1,'msg'=>'删除成功'];
        }else{
            $retArray = ['rel'=>0,'msg'=>'删除失败'];
        }
        return json($retArray);
    }
}